﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Thor.Domain;

namespace Thor.Repository.Azure.Table
{
    /// <summary>
    /// A repository that stores invalid logon entities.
    /// </summary>
    public class InvalidLogonRepository : TableRepositoryBase, IInvalidLogonRepository
    {
        #region Fields

        private InvalidLogonContext _context;

        #endregion

        #region Constructors

        public InvalidLogonRepository(Dictionary<string, object> repositoryParameters)
            : base(repositoryParameters)
        {
            _context = new InvalidLogonContext(StorageAccount.TableEndpoint.AbsoluteUri, StorageAccount.Credentials);
        }

        #endregion

        #region IAdministratorAccountRepository Members

        /// <summary>
        /// Inserts an invalid logon entity into the repository.
        /// </summary>
        /// <param name="invalidLogon"></param>
        public void Insert(InvalidLogon invalidLogon)
        {
            _context.Insert(CreateRowFromInvalidLogon(invalidLogon));
        }

        /// <summary>
        /// Updates an invalid logon entity in the repository.
        /// </summary>
        /// <param name="invalidLogon"></param>
        public void Update(InvalidLogon invalidLogon)
        {
            _context.Update(CreateRowFromInvalidLogon(invalidLogon));
        }

        /// <summary>
        /// Deletes an invalid logon entity from the repository.
        /// </summary>
        /// <param name="invalidLogon"></param>
        public void Delete(InvalidLogon invalidLogon)
        {
            _context.Delete(CreateRowFromInvalidLogon(invalidLogon));
        }

        /// <summary>
        /// Fetches all invalid logon entities from the repository.
        /// </summary>
        /// <returns></returns>
        public List<InvalidLogon> Fetch()
        {
            List<InvalidLogonRow> rows = _context.GetRows();
            List<InvalidLogon> invalidLogons = null;

            if (rows != null)
            {
                invalidLogons = new List<InvalidLogon>();

                foreach (InvalidLogonRow row in rows)
                    invalidLogons.Add(CreateInvalidLogonFromRow(row));
            }

            return invalidLogons;
        }

        /// <summary>
        /// Fetches a invalid logon entity from the repository.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public InvalidLogon Fetch(Guid id)
        {
            InvalidLogonRow row = _context.GetRow(id);

            if (row != null)
                return CreateInvalidLogonFromRow(row);
            else
                return null;
        }

        /// <summary>
        /// Creates a row from the POCO object.
        /// </summary>
        /// <param name="invalidLogon"></param>
        /// <returns></returns>
        private InvalidLogonRow CreateRowFromInvalidLogon(InvalidLogon invalidLogon)
        {
            return new InvalidLogonRow()
            {
                ApplicationName = ApplicationName,
                PartitionKey = ApplicationName,
                RowKey = invalidLogon.Id.ToString(),
                UserName = invalidLogon.UserName,
                AttemptCount = invalidLogon.AttemptCount,
                FirstAttemptTimestamp = invalidLogon.FirstAttemptTimestamp,
                LastAttemptTimestamp = invalidLogon.LastAttemptTimestamp,
                Id = invalidLogon.Id
            };
        }

        /// <summary>
        /// Creates a POCO object from the row.
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private InvalidLogon CreateInvalidLogonFromRow(InvalidLogonRow row)
        {
            return new InvalidLogon()
            {
                UserName = row.UserName,
                Id = row.Id,
                AttemptCount = row.AttemptCount,
                FirstAttemptTimestamp = row.FirstAttemptTimestamp,
                LastAttemptTimestamp = row.LastAttemptTimestamp
            };
        }

        #endregion
    }
}
